package com.hqshuke.exchange.system.config.logging;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
@Slf4j
public class LoggingAspect {


    @Around("execution(* com.hqshuke.exchange.system.service.impl..*(..))")
    public Object logMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        log.info("进入方法：{}.{} ,参数: {}", className,methodName, Arrays.toString(args));

        Object result;
        try {
            result = joinPoint.proceed();
            log.info("方法 {}.{} 返回: {}", className,methodName, result);
        } catch (Throwable throwable) {
            log.error("方法 {}.{} 抛出异常: {}", className,methodName, throwable.getMessage());
            throw throwable; // 重新抛出异常以确保事务回滚
        }
        return result;
    }
}
